using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Devv.Core.Utils;
using Devv.Threeter.Twitter;
using TweetSharp;

namespace Devv.Threeter
{
	static class Program
	{
		#region Properties

		public static Accounts CurrentAccounts { get; set; }
		public static FormPost FormPostInstance { get; set; }

		#endregion

		#region Autorun

		public static Boolean AutoRun
		{
			get
			{
				try
				{
					return (RegistryUtil.GetAutoRun(GlobalMessages.AppName).ToLower().Contains(Path.GetFileName(Application.ExecutablePath).ToLower()));
				}
				catch
				{
					return false;
				}
			}
		}

		#endregion

		#region Main methods

		[STAThread]
		static void Main()
		{
			Core.Erroo.Config.LoadFromXml();
			Core.Erroo.Config.LogPath = Application.UserAppDataPath + "\\ErrorLogs";
			Core.Erroo.Config.DoShowDialog = Properties.Settings.Default.Debug;

			Core.Crypto.Config.Key = Core.Crypto.Hash.MD5(Environment.UserName);

			Application.ThreadException += Core.Erroo.ErrorHandler.OnThreadException;
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			TwitterManager.Load();

			if (Properties.Settings.Default.FirstRun)
			{
				MaintenanceManager.UpgradeFromPrevious();

				Properties.Settings.Default.Upgrade();
				Properties.Settings.Default.FirstRun = true;
			}

			if (!String.IsNullOrEmpty(Properties.Settings.Default.Culture))
			{
				SetCulture(new CultureInfo(Properties.Settings.Default.Culture), false);
			}

			CurrentAccounts = new Accounts();
			CurrentAccounts.Load();

			FormPostInstance = new FormPost();
			Application.Run(FormPostInstance);
		}

		#endregion

		#region Secondary forms

		internal static void ShowFriendUpdates()
		{
			if (TwitterManager.LoggedAccount != null)
			Process.Start(TwitterManager.GetCurrentAccountUrl());
		}

		internal static void ShowFormOptions(Form caller, Boolean closeAfterLogin)
		{
			caller.Hide();

			foreach (Form form in Application.OpenForms)
			{
				if (form.Name == "FormOptions")
				{
					form.BringToFront();
					form.Focus();
					return;
				}
			}

			using (FormOptions form = new FormOptions())
			{
				form.CloseAfterLogin = closeAfterLogin;
				form.ShowDialog();
			}
		}

		internal static void ShowFormAccounts()
		{
			ShowFormAccounts(FormPostInstance);
		}

		internal static void ShowFormAccounts(Form caller)
		{
			caller.Hide();

			foreach (Form form in Application.OpenForms)
			{
				if (form.Name == "FormAccounts")
				{
					form.BringToFront();
					form.Focus();
					return;
				}
			}

			using (FormAccounts form = new FormAccounts())
			{
				form.ShowDialog();
			}
		}

		#endregion

		#region Language and culture

		public static void SetCulture(CultureInfo culture, Boolean started)
		{
			Application.CurrentCulture = culture;

			Thread.CurrentThread.CurrentCulture = culture;
			Thread.CurrentThread.CurrentUICulture = culture;

			if (started)
			{
				foreach (Form form in Application.OpenForms)
				{
					ApplyControlResources(new ComponentResourceManager(form.GetType()), form, culture);
					form.Refresh();
				}
			}
		}

		private static void ApplyControlResources(ComponentResourceManager resourceManager, Control control, CultureInfo culture)
		{
			if (control is Form)
			{
				resourceManager.ApplyResources(control, "$this", culture);
			}
			else
			{
				resourceManager.ApplyResources(control, control.Name, culture);
			}

			foreach (Control childControl in control.Controls)
			{
				ApplyControlResources(resourceManager, childControl, culture);
			}
		}

		#endregion
	}
}